import yaml
import os

dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# 读取yaml文件,读取某一个key-value
def read_yaml(yaml_path, key):
    with open(dir_path + yaml_path, encoding="utf-8") as f:
        value = yaml.load(stream=f, Loader=yaml.FullLoader)
        return value[key]


# 写入yaml文件
def write_yaml(yaml_path, data):
    with open(dir_path + yaml_path, encoding="utf-8", mode="a") as f:
        yaml.dump(data, stream=f, allow_unicode=True)


# 清空yaml文件
def clear_yaml(yaml_path):
    with open(dir_path + yaml_path, encoding="utf-8", mode="w") as f:
        f.truncate()


# 读取yaml文件，文件的全部的key-value
def read_all_yaml(yaml_path):
    with open(dir_path + yaml_path, encoding="utf-8", mode="r") as f:
        data = yaml.load(stream=f, Loader=yaml.FullLoader)
        return data
